package net.huashitong.user.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.responseModel.MessageModel;
import net.huashitong.user.entity.Message;
import net.huashitong.user.filter.MessageFilter;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

import static net.huashitong.utils.UtilsTools.getCurrentDateTime1;

@Repository
public class MessageDao extends StringDao<Message> {
    public List<Message> getMessageInfo(MessageFilter filter) {
        GenericQuery query = createQuery("obj");
        if (StringUtils.isNotBlank(filter.getProjectName())) {
            query.append(" and obj.projectName like:projectName").setParam("projectName",
                    StringUtils.getLikewords(filter.getProjectName()));
        }
        if (StringUtils.isNotBlank(filter.getPhone())) {
            query.append(" and obj.phone like:phone").setParam("phone",
                    StringUtils.getLikewords(filter.getPhone()));
        }
        if (StringUtils.isNotBlank(filter.getType())) {
            query.append(" and obj.type =:type").setParam("type",
                    filter.getType());
        }
        if (StringUtils.isNotBlank(filter.getBranchType())) {
            query.append(" and obj.branchType =:branchType").setParam("branchType",
                    filter.getBranchType());
        }
        if (StringUtils.isNotBlank(filter.getResDept())) {
            query.append(" and obj.resDept like:resDept").setParam("resDept",
                    StringUtils.getLikewords(filter.getResDept()));
        }
        if (StringUtils.isNotBlank(filter.getRecipients())) {
            query.append(" and obj.recipients like:recipients").setParam("recipients",
                    StringUtils.getLikewords(filter.getRecipients()));
        }
        if (StringUtils.isNotBlank(filter.getText())) {
            query.append(" and obj.text like:text").setParam("text",
                    StringUtils.getLikewords(filter.getText()));
        }

        //==================================模糊搜索==================================
        if(StringUtils.isNotBlank(filter.getSearchWord())){
            query.append(" and (obj.projectName like:projectName or " +
                    "obj.phone like:phone or "+
                    "obj.resDept like:resDept or " +
                    "obj.recipients like:recipients or " +
                    "obj.text like:text) ").setParam("projectName",
                    StringUtils.getLikewords(filter.getSearchWord())).setParam("phone",
                    StringUtils.getLikewords(filter.getSearchWord())).setParam("resDept",
                    StringUtils.getLikewords(filter.getSearchWord())).setParam("recipients",
                    StringUtils.getLikewords(filter.getSearchWord())).setParam("text",
                    StringUtils.getLikewords(filter.getSearchWord()));
        }
        //==================================模糊搜索==================================


        //如果时间已经设置
        if (StringUtils.isNotBlank(filter.getEndDate()) &&
                StringUtils.isNotBlank(filter.getBeginDate())) {
            query.append(" and unix_timestamp( obj.createDate ) between unix_timestamp( '" + filter.getBeginDate() + "') and unix_timestamp( '" + filter.getEndDate() + "' )");
        }
        if (StringUtils.isNotBlank(filter.getEndDate()) &&
                StringUtils.isBlank(filter.getBeginDate())) {
            query.append(" and unix_timestamp( obj.createDate ) between unix_timestamp( '" + filter.getBeginDate() + "') and unix_timestamp( '" + getCurrentDateTime1() + "' )");
        }

        if (ObjectUtils.isNotBlank(filter.getPaging())) {
            if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
                query.append(" ORDER BY  str_to_date( CONCAT(obj.createDate,' ',obj.createTime),'%Y-%m-%d %H:%i:%s') desc  ");
            }
        }
        return query.listResult(filter);
    }

    public List<MessageModel> getMessageByDate(String beginDate, String endDate) {
        Session session = sessionFactory.openSession();
        StringBuilder sql = new StringBuilder("SELECT project_name projectName,type type,res_dept resDept,recipients " +
                "recipients,phone phone,text text,create_date createDate,create_time createTime FROM t_bnp_message");
        if (StringUtils.isNotBlank(beginDate) || StringUtils.isNotBlank(endDate)) {
            sql.append(" WHERE create_date BETWEEN '" + beginDate + "' AND '" + endDate + "'");
        }
        Query query;
        query = session.createSQLQuery(sql.toString());
        query.setResultTransformer(Transformers.aliasToBean(MessageModel.class));
        List<MessageModel> messageList = (List<MessageModel>) query.list();
        return messageList;
    }

    public List<Message> getMessageModels(String projectName, String nodeId, String phone) {
        GenericQuery query = createQuery("obj");
        query.append(" and obj.projectName =:projectName").setParam("projectName", projectName);
        query.append(" and obj.nodeId =:nodeId").setParam("nodeId", nodeId);
        query.append(" and obj.phone =:phone").setParam("phone", phone);
        return query.listResult();
    }

    public List<Message> getMessageInfoNoPage(MessageFilter filter) {
        GenericQuery query = createQuery("obj");
        if (StringUtils.isNotBlank(filter.getProjectName())) {
            query.append(" and obj.projectName like:projectName").setParam("projectName",
                    StringUtils.getLikewords(filter.getProjectName()));
        }
        if (StringUtils.isNotBlank(filter.getPhone())) {
            query.append(" and obj.phone like:phone").setParam("phone",
                    StringUtils.getLikewords(filter.getPhone()));
        }
        if (StringUtils.isNotBlank(filter.getType())) {
            query.append(" and obj.type =:type").setParam("type",
                    filter.getType());
        }
        if (StringUtils.isNotBlank(filter.getBranchType())) {
            query.append(" and obj.branchType =:branchType").setParam("branchType",
                    filter.getBranchType());
        }
        if (StringUtils.isNotBlank(filter.getNodeId())) {
            query.append(" and obj.nodeId =:nodeId").setParam("nodeId",
                    filter.getNodeId());
        }
        if (StringUtils.isNotBlank(filter.getResDept())) {
            query.append(" and obj.resDept like:resDept").setParam("resDept",
                    StringUtils.getLikewords(filter.getResDept()));
        }
        if (StringUtils.isNotBlank(filter.getRecipients())) {
            query.append(" and obj.recipients like:recipients").setParam("recipients",
                    StringUtils.getLikewords(filter.getRecipients()));
        }
        if (StringUtils.isNotBlank(filter.getText())) {
            query.append(" and obj.text like:text").setParam("text",
                    StringUtils.getLikewords(filter.getText()));
        }
        //如果时间已经设置
        if (StringUtils.isNotBlank(filter.getEndDate()) &&
                StringUtils.isNotBlank(filter.getBeginDate())) {
            query.append(" and unix_timestamp( obj.createDate ) between unix_timestamp( '" + filter.getBeginDate() + "') and unix_timestamp( '" + filter.getEndDate() + "' )");
        }
        if (StringUtils.isNotBlank(filter.getEndDate()) &&
                StringUtils.isBlank(filter.getBeginDate())) {
            query.append(" and unix_timestamp( obj.createDate ) between unix_timestamp( '" + filter.getBeginDate() + "') and unix_timestamp( '" + getCurrentDateTime1() + "' )");
        }
//        if (ObjectUtils.isNotBlank(filter.getPaging())) {
//            if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
//                query.append(" ORDER BY  str_to_date( CONCAT(obj.createDate,' ',obj.createTime),'%Y-%m-%d %H:%i:%s') desc  ");
//            }
//        }
        query.append(" ORDER BY  str_to_date( CONCAT(obj.createDate,' ',obj.createTime),'%Y-%m-%d %H:%i:%s') desc  ");
        return query.listResult();
    }

    public List<Message> queryMessageByIds(List<String> ids){
        GenericQuery query = createQuery("obj");
        query.append("and obj.sysId in(:ids)").setParamList("ids", ids);
        return query.listResult();
    }
}
